package com.xckj.teacher.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.teacher.R;
import com.xckj.teacher.bean.TextbookBean;
import com.xckj.teacher.ui.adapter.GradeAdapter;
import com.xckj.teacher.ui.adapter.VersionNameAdapter;

import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 选择教材的Dialog
 * @Date: 2024/6/11 21:06
 */
public class SelectedTextbookDialog extends Dialog {
    private TextView tvClose;
    private RecyclerView recGrade;
    private RecyclerView recVersionName;
    private List<TextbookBean> textbookBeans;
    private OnCloseClickListener closeClickListener;
    private VersionNameAdapter versionNameAdapter;
    private OnItemClickListener itemClickListener;

    public SelectedTextbookDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public void setCloseClickListener(OnCloseClickListener closeClickListener) {
        this.closeClickListener = closeClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setData(List<TextbookBean> textbookBeans) {
        this.textbookBeans = textbookBeans;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_selected_textbook);
        setCanceledOnTouchOutside(false);
        initView();
        initGrade();
        initVersionName();
        setListener();
    }

    private void initGrade() {
        recGrade.setLayoutManager(new LinearLayoutManager(recGrade.getContext()));
        GradeAdapter gradeAdapter = new GradeAdapter();
        textbookBeans.get(0).isClickState = true;
        gradeAdapter.setData(textbookBeans);
        recGrade.setAdapter(gradeAdapter);
        gradeAdapter.setOnItemClickListener((view, position) -> {
            versionNameAdapter.setData(textbookBeans.get(position).children);
        });
    }

    private void initVersionName() {
        recVersionName.setLayoutManager(new LinearLayoutManager(recVersionName.getContext()));
        versionNameAdapter = new VersionNameAdapter();
        versionNameAdapter.setData(textbookBeans.get(0).children);
        recVersionName.setAdapter(versionNameAdapter);
        versionNameAdapter.setOnItemClickListener(children -> {
            itemClickListener.onItemClick(children);

        });
    }

    private void initView() {
        tvClose = findViewById(R.id.tv_close);
        recGrade = findViewById(R.id.rec_grade);
        recVersionName = findViewById(R.id.rec_version_name);
    }

    private void setListener() {
        tvClose.setOnClickListener(view -> {
            if (null != closeClickListener) {
                closeClickListener.onCloseClick();
            }
        });
    }


    /**
     * 关闭按钮
     */
    public interface OnCloseClickListener {
        /**
         * 取消
         */
        void onCloseClick();
    }

    public interface OnItemClickListener {
        /**
         * Item的点击事件
         *
         * @param children 当前选中的数据
         */
        void onItemClick(TextbookBean.Children children);
    }
}
